/**
 * 
 */
package com.roc.util.constant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.roc.bean.CardBin;

/**
 * @author Administrator
 * 
 */
public class ConfigProperties {
	private  static Logger logger = LogManager.getLogger(ConfigProperties.class);
	private final static String GB_STR="GB_Area_MAP";
	private final static String UN_STR="UnionPay_Area";
	static final Map<String, String> systemProperties = new HashMap<String, String>();
	//代理商类别
	static final Map<String, String> agentTypeProperties = new HashMap<String, String>();
	//角色
	static final Map<String, String> roleTypeProperties = new HashMap<String, String>();
	//省份
	static final Map<String, String> areaCodeProperties = new HashMap<String, String>();
	//MCC分类
	static final Map<String, String> mccTypeProperties = new HashMap<String, String>();
	//MCC分类
	static final Map<String, String> hyMccTypeProperties = new HashMap<String, String>();
	//省份
	static final Map<String, String> unionProviceProperties = new HashMap<String, String>();
	//省份
	public static final Map<String, String> gbProviceProperties = new HashMap<String, String>();
	//城市
	static final Map<String, String> unionCityProperties = new HashMap<String, String>();
	//城市
	public static final Map<String, String> gbCityProperties = new HashMap<String, String>();
	//地区
	static final Map<String, String> unionAreaProperties = new HashMap<String, String>();
	//地区
	static final Map<String, String> gbAreaProperties = new HashMap<String, String>();
		
	//交易类型
	static final Map<String, String> tranTypeProperties = new HashMap<String, String>();

	//云商宝品牌
	static final Map<String, String> ysbBrandProperties = new HashMap<String, String>();

	//云商宝型号
	static final List<Object> ysbModelProperties= new ArrayList<Object>();
	
	//银行
	static final Map<String, String> bankListProperties = new HashMap<String, String>();
	
	//卡BIN
	static final List<Object> cardBinListProperties=new ArrayList<Object>(); 
	
	//数据字典
	static final List<Object> sysDbListProperties= new ArrayList<Object>();
	
	//数据字典2
	static final List<Object> layerDBListProperties= new ArrayList<Object>();
	
	static final Map<String, String> provinces = new TreeMap<String, String>();
	
	static final Map<String, String> transTypes = new TreeMap<String, String>();
	static final Map<String, String> reversalTypes = new TreeMap<String, String>();
	static final Map<String, String> respCodeTypes = new TreeMap<String, String>();
	static final Map<String, String> respCodeQueryTypes = new TreeMap<String, String>();
	
	static final Map<String,String> POSBrandMap=new TreeMap<String, String>();
	static final Map<String,String> POSNameMap=new TreeMap<String, String>();
	static final Map<String,String> POSTypeMap=new TreeMap<String, String>();

	static final Map<String,String> POSBrandLengthMap=new TreeMap<String, String>();
	static{
		
		for (BcTranType b : BcTranType.values()){  
			transTypes.put(b.getCode(),b.getName());
		}
		
		for (ReversalType b : ReversalType.values()){  
			reversalTypes.put(b.getCode(),b.getName());
		}
		for (RespCode b : RespCode.values()){  
			respCodeQueryTypes.put(b.getCode(),b.getName());
		}
		if(respCodeTypes!=null){
			respCodeTypes.put(RespCode.RESP_CODE_01.getCode(), RespCode.RESP_CODE_01.getName());
			respCodeTypes.put(RespCode.RESP_CODE_03.getCode(), RespCode.RESP_CODE_03.getName());
			respCodeTypes.put(RespCode.RESP_CODE_12.getCode(), RespCode.RESP_CODE_12.getName());
			respCodeTypes.put(RespCode.RESP_CODE_13.getCode(), RespCode.RESP_CODE_13.getName());
			respCodeTypes.put(RespCode.RESP_CODE_51.getCode(), RespCode.RESP_CODE_51.getName());
			respCodeTypes.put(RespCode.RESP_CODE_55.getCode(), RespCode.RESP_CODE_55.getName());
			respCodeTypes.put(RespCode.RESP_CODE_57.getCode(), RespCode.RESP_CODE_57.getName());
			
			respCodeTypes.put(RespCode.RESP_CODE_61.getCode(), RespCode.RESP_CODE_61.getName());
			respCodeTypes.put(RespCode.RESP_CODE_75.getCode(), RespCode.RESP_CODE_75.getName());
			respCodeTypes.put(RespCode.RESP_CODE_96.getCode(), RespCode.RESP_CODE_96.getName());
			respCodeTypes.put(RespCode.RESP_CODE_97.getCode(), RespCode.RESP_CODE_97.getName());
			respCodeTypes.put(RespCode.RESP_CODE_99.getCode(), RespCode.RESP_CODE_99.getName());

			respCodeTypes.put(RespCode.RESP_CODE_A0.getCode(), RespCode.RESP_CODE_A0.getName());
			
			respCodeTypes.put(RespCode.RESP_CODE_AB.getCode(), RespCode.RESP_CODE_AB.getName());
			respCodeTypes.put(RespCode.RESP_CODE_S1.getCode(), RespCode.RESP_CODE_S1.getName());
			respCodeTypes.put(RespCode.RESP_CODE_9999.getCode(), RespCode.RESP_CODE_9999.getName());
			
		}
		
		for (TerminalBrand b : TerminalBrand.values()){  
			POSBrandMap.put(b.getCode(),b.getName());
		}	
		
		
		for (TerminalName b : TerminalName.values()){  
			POSNameMap.put(b.getName(),b.getName());
		}
		
		
		for (TerminalType b : TerminalType.values()){  
			POSTypeMap.put(b.getCode(),b.getName());
		}

		for (TerminalBrandLength b : TerminalBrandLength.values()){  
			POSBrandLengthMap.put(b.getName(),b.getCode());
		}
	}
	public static String getSystemConfig(String key) {
		return systemProperties.get(key);
	}

	public static List<Object> getCardBin(){
		return cardBinListProperties;
	}
	
	public static List<Object> getSysDB(){
		return sysDbListProperties;
	}
	public static String getProvinceName(String provinceCode){
		return provinces.get(provinceCode);
	}
	
	public static String getRoleType(String key) {
		return roleTypeProperties.get(key);
	}

	public static String getAgentType(String key) {
		return agentTypeProperties.get(key);
	}
	public static String getMccType(String key) {
		return mccTypeProperties.get(key);
	}
	public static String getHyMccType(String key) {
		return hyMccTypeProperties.get(key);
	}
	public static String getAreaCode(String key) {
		return unionAreaProperties.get(key);
	}
	public static String getTranType(String key) {
		return tranTypeProperties.get(key);
	}
	public static String getBankList(String key) {
		return bankListProperties.get(key);
	}
	public static String getRevasalTypeName(String key){
		return reversalTypes.get(key);
	}
	public static String getResCodeName(String key){
		return respCodeTypes.get(key);
	}
	public static Map<String, String> getRoleTypeProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(roleTypeProperties);
		return result;
	}
	public static Map<String, String> getAgentTypeProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(agentTypeProperties);
		return result;
	}
	public static Map<String, String> getMccTypeProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(mccTypeProperties);
		return result;
	}
	public static Map<String, String> getHyMccTypeProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(hyMccTypeProperties);
		result.remove("6010"); //移除   6010-金融机构
		return result;
	}
	public static Map<String, String> getTranTypeProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(tranTypeProperties);
		return result;
	}
	public static Map<String, String> getBankListProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(bankListProperties);
		return result;
	}
	public static Map<String, String> getProvinces(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(provinces);
		return result;
	}
	public static List<Object> getCardBin1(){
		List<Object> cardBinList=new ArrayList<Object>();
		cardBinList.add((CardBin)cardBinListProperties);
		return cardBinList;
	}

	public static Map<String, String> getYsbBrandProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(ysbBrandProperties);
		return result;
	}

	public static List<Map<String, Object>> getYsbModelProperties( String p_code ){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		;
		for (Object object : ysbModelProperties) {
			Object[] obj = (Object[])object ; 
			
			String pCode = obj[0].toString();
			if (null != pCode && pCode.equals(p_code)){
				Map<String,Object> map = new TreeMap<String, Object>();
			
				String dicCode = obj[1].toString();
				String dicName = obj[2].toString();
				
				map.put("CODE", dicCode);
				map.put("NAME", dicName);
				result.add(map);
			}
		}
		return result;
	}
	
	
	public static Map<String, String> getTransTypes(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(transTypes);
		return result;
	}
	public static Map<String, String> getReversalTypes(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(reversalTypes);
		return result;
	}
	public static Map<String, String> getRespCodeTypes(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(respCodeTypes);
		return result;
	}
	
	public static Map<String, String> getRespCodeQueryTypes(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(respCodeQueryTypes);
		return result;
	}
	
	public static Map<String, String> getPOSBrand(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(POSBrandMap);
		return result;
	}
	public static Map<String, String> getPOSType(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(POSTypeMap);
		return result;
	}
	public static Map<String, String> getPOSName(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(POSNameMap);
		return result;
	}
	public static Map<String, String> getPOSBrandLengthMap(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(POSBrandLengthMap);
		return result;
	}
	public static String getPOSBrandLength(String key){
		String value = POSBrandLengthMap.get(key);
		return value;
	}
	/**
	 * 处理数据字典
	 * */
	public static Map<String, String> getSysDB1(String type,boolean isSplit){
		Map<String, String> sysMap=new LinkedHashMap<String, String>();
		Iterator<?> iter=sysDbListProperties.iterator();
		while(iter.hasNext()){
			Object[] obj=(Object[])iter.next();
			if(obj[0].equals(type)){
				if(isSplit){
					sysMap.put( String.valueOf(obj[1]), String.valueOf(obj[2]).split("_")[1]);//汇宜坑爹的数据字典，在此分割只取汉字部分
				}else{
					sysMap.put( String.valueOf(obj[1]), String.valueOf(obj[2]));//汇宜坑爹的数据字典，在此分割只取汉字部分
				}
				
			}
		}
		return sysMap;
	}
	//处理数据字典国标省份
        public static Map<String, String> getGBProviceProperties(){
            Map<String, String> sysMap=new HashMap<String, String>();
            
            for (Map.Entry<String, String> entry : gbProviceProperties.entrySet()) { 
                if(entry.getValue().contains("_")){
                    sysMap.put(entry.getKey(), entry.getValue().substring(3));
                }else{
                    sysMap.put(entry.getKey(), entry.getValue());
                }
            }
            return sysMap;
        }
        
        public static Map<String, String> getGBCityProperties(String proviceCode){
    		Map<String, String> result = new TreeMap<String, String>();
    		if(null!=proviceCode){//城市不为空
    			Iterator<?> iter=layerDBListProperties.iterator();
    			while(iter.hasNext()){
    				Object[] obj=(Object[])iter.next();
    				String value=String.valueOf(obj[4]);//父类值
    				if(proviceCode.equals(value)){
    					String akey= String.valueOf(obj[0]);
    					String avalue= String.valueOf(obj[1]);
    					if(avalue.contains("_")){
    						result.put(akey, avalue.substring(3));
    					}else{
    						result.put(akey, avalue);
    					}
    				}
    			}
    		}
    		return result;
    	}
        
        public static Map<String, String> getGBAreaProperties(String  cityCode){
    		Map<String, String> result = new TreeMap<String, String>();
    		if(null!=cityCode){//城市不为空
    			Iterator<?> iter=layerDBListProperties.iterator();
    			while(iter.hasNext()){
    				Object[] obj=(Object[])iter.next();
    				String value=String.valueOf(obj[4]);//父类值
    				if(cityCode.equals(value)){
    					String akey= String.valueOf(obj[0]);
    					String avalue= String.valueOf(obj[1]);
    					if(avalue.contains("_")){
    						result.put(akey, avalue.substring(3));
    					}else{
    						result.put(akey, avalue);
    					}
    				}
    			}
    		}
    		return result;
    	}
        
	/**
	 * 处理数据字典pos型号
	 * */
	public static Map<String, String> getSyspostype(String type,String brandno,boolean isSplit){
		Map<String, String> sysMap=new LinkedHashMap<String, String>();
		Iterator<?> iter=layerDBListProperties.iterator();
		while(iter.hasNext()){
			Object[] obj=(Object[])iter.next();
			if(obj[2].equals(type)){
				if(obj[4].equals(brandno)){
					if(isSplit){
						sysMap.put( String.valueOf(obj[0]), String.valueOf(obj[1]).split("_")[1]);//汇宜坑爹的数据字典，在此分割只取汉字部分
					}else{
						sysMap.put( String.valueOf(obj[0]), String.valueOf(obj[1]));//汇宜坑爹的数据字典，在此分割只取汉字部分
					}
				}
			}
		}
		return sysMap;
	}
	/**
	 * 处理数据字典
	 * */
	public static void handleFirstSysDB(){
		Iterator<?> iter=layerDBListProperties.iterator();
		while(iter.hasNext()){
			Object[] obj=(Object[])iter.next();
			if(String.valueOf(obj[2]).trim().equals(UN_STR)){ //un 地区码
				if(String.valueOf(obj[3]).trim().equals("1")){
					//系统暂时没有海南、青海、安徽地区     海南的type 为-UnionPay_Area_CLOSE
					unionProviceProperties.put(String.valueOf(obj[0]), String.valueOf(obj[1]));//省份
					
				}else if(String.valueOf(obj[3]).trim().equals("2")){
					unionCityProperties.put(String.valueOf(obj[0]), String.valueOf(obj[1]));//城市
				}else{
					unionAreaProperties.put(String.valueOf(obj[0]), String.valueOf(obj[1]));//区县
				}
			}else if(String.valueOf(obj[2]).trim().equals(GB_STR)){// gb 地区码
				if(String.valueOf(obj[3]).trim().equals("0")){
					gbProviceProperties.put(String.valueOf(obj[0]), String.valueOf(obj[1]));//省份
				}else if(String.valueOf(obj[3]).trim().equals("1")){
					gbCityProperties.put(String.valueOf(obj[0]), String.valueOf(obj[1]));//城市
				}else{
					gbAreaProperties.put(String.valueOf(obj[0]), String.valueOf(obj[1]));//区县
				}
			} 
		}
		logger.info("-----gb provice size["+gbProviceProperties.size()+"]----");
		logger.info("-----gb city size["+gbCityProperties.size()+"]----");
		logger.info("-----gb area size["+gbAreaProperties.size()+"]----");
		logger.info("-----un provice size["+unionProviceProperties.size()+"]----");
		logger.info("-----un city size["+unionCityProperties.size()+"]----");
		logger.info("-----un area size["+unionAreaProperties.size()+"]----");
	}
	/*//处理gb省份
	public static Map<String, String> getGBProviceProperties(){
		Map<String, String> sysMap=new LinkedHashMap<String, String>();
		Iterator<?> iter=layerDBListProperties.iterator();
		while(iter.hasNext()){
			Object[] obj=(Object[])iter.next();
			if(obj[0].equals(type)){
				if(isSplit){
					sysMap.put( String.valueOf(obj[1]), String.valueOf(obj[2]).split("_")[1]);//汇宜坑爹的数据字典，在此分割只取汉字部分
				}else{
					sysMap.put( String.valueOf(obj[1]), String.valueOf(obj[2]));//汇宜坑爹的数据字典，在此分割只取汉字部分
				}
				
			}
		}
		return sysMap;
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(gbProviceProperties);
		return result;
	}*/
	
	//处理银联省份
	public static Map<String, String> getUnionProviceProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(unionProviceProperties);
		return result;
	}
	public static Map<String, String> getUnionCityProperties(String proviceCode){
		Map<String, String> result = new TreeMap<String, String>();
		if(null!=proviceCode){//城市不为空
			Iterator<?> iter=layerDBListProperties.iterator();
			while(iter.hasNext()){
				Object[] obj=(Object[])iter.next();
				String value=String.valueOf(obj[4]);//父类值
				if(proviceCode.equals(value) && String.valueOf(obj[2]).equals(UN_STR)  && Long.parseLong( String.valueOf(obj[3]))>1){
					result.put(String.valueOf(obj[0]), String.valueOf(obj[1]));
					continue;
				}
			}
		}else{
			result.putAll(unionCityProperties);
		}
		
		return result;
	}
	public static Map<String, String> getUnAreaProperties(String  cityCode){
		Map<String, String> result = new TreeMap<String, String>();
		if(null!=cityCode){//城市不为空
			Iterator<?> iter=layerDBListProperties.iterator();
			while(iter.hasNext()){
				Object[] obj=(Object[])iter.next();
				String value=String.valueOf(obj[4]);//父类值
				if(cityCode.equals(value)){
					result.put(String.valueOf(obj[0]), String.valueOf(obj[1]));
					continue;
				}
			}
		}else{
			result.putAll(unionAreaProperties);
		}
		return result;
	}
	public static Map<String, String> getAreaCodeProperties(){
		Map<String, String> result = new TreeMap<String, String>();
		result.putAll(unionProviceProperties);
		return result;
	}
	/**
	 * 处理pos功能授权
	 * */
	public static Map<String, String> getPosFunction(Map<String, String> posMap){
		Map<String, String> sysMap=new LinkedHashMap<String, String>();
		Iterator<?> iter=posMap.entrySet().iterator();
		while(iter.hasNext()){
			Map.Entry<?,?> entry = (Map.Entry<?,?>) iter.next();
			String key = (String) entry.getKey();
			String value = (String) entry.getValue();
			int keys=Integer.parseInt(key);
			switch (keys) {
				case 1011: //消费
					sysMap.put(key, value);
				case 1001: //余额查询
					sysMap.put(key, value);
				case 2011: //消费撤销
					sysMap.put(key, value);
				case 4011: //消费冲正
					sysMap.put(key, value);
				case 1021: //预授权
					sysMap.put(key, value);
				case 1031: //预授权完成
					sysMap.put(key, value);
				case 1171: //实时收款  t0
					sysMap.put(key, value);
			}
			 
		}
		return sysMap;
	}
	
	public static Map<String,String> getUNCityAreaMap(){
		Map<String,String> noDealMap = new HashMap<String, String>();
		Map<String,String> result = new HashMap<String, String>();
		noDealMap.putAll(unionAreaProperties);
		noDealMap.putAll(unionCityProperties);
		for(Entry<String,String> en:noDealMap.entrySet()){
			if(en.getValue().contains("_")){
				result.put(en.getKey(), en.getValue().substring(en.getValue().lastIndexOf("_")+1));
			}else{
				result.put(en.getKey(), en.getValue());
			}
		}
		
		return result;
	}
	
	/**
	 * 将原因码处理成code_value形式
	 * @return
	 */
	public static Map<String,String> dealReasonCdMap(){
		Map<String,String> reasonCodeMap = new HashMap<String, String>();
		Map<String,String> reasonCdMap = ConfigProperties.getSysDB1("RISK_CONTROL_D",false);
		Iterator it = reasonCdMap.entrySet().iterator();
		while(it.hasNext()){
			Entry en = (Entry) it.next();
			reasonCodeMap.put(String.valueOf(en.getKey()), en.getKey()+"_"+en.getValue());
		}
		return reasonCodeMap;
	}
}
